<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class RekamMedis extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->_data = array ();
        $this->load->model ( 'registrasi_model' );
        isController('klinik','rekammedis');
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    public function index() {
        redirect ( 'klinik/rekamMedis/view' );
    }
    protected function _executeView() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'action' ) );
        // delete action
        if (isAccess('klinik','rekammedis','delete') && $array ['action'] == 'delete' && $array ['id']) {
            if ($this->registrasi_model->delete($array['id'])) {
                $this->_data['errorMessage'] =  'Berhasil Delete Rekam Medis' ;
                $this->_data['isSuccess'] =  true ;
            } else {
                $this->_data['errorMessage'] =  'Gagal Delete Rekam Medis' ;
            }
        }
    }
    public function view() {
    	if (! isAccess ( 'klinik', 'rekammedis', 'view'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'pasienId', 'frdate', 'todate', 'action', 'page' ) );
        $this->_executeView ();
        $array['page'] = (int) $array['page'];
        
        //Uri
        $pasienIdVal = $this->input->post('pasienId') ? $this->input->post('pasienId') : 'all';
        $array['pasienId'] = get_data($array,'pasienId') ? get_data($array,'pasienId') : $pasienIdVal;
        
        $array['frdate'] = get_data($array,'frdate') ? get_data($array,'frdate') : 'all';
        $array['todate'] = get_data($array,'todate') ? get_data($array,'todate') : 'all';
        
        //Variable
        $this->load->model('pasien_model');
        $this->_data['listPasien'] = $this->pasien_model->getList(100, 0);
        
        $frdate = $this->input->post('frdate') ? $this->input->post('frdate') : $array['frdate'];
        $this->_data['frdate'] = $frdate;
        
        $todate = $this->input->post('todate') ? $this->input->post('todate') : $array['todate'];
        $this->_data['todate'] = $todate;
        
        //Where
        $where = array();
        $where['rmStatus'] = array( 'Selesai' );
        if( $array['pasienId'] != 'all' )
        	$where['pasienId'] = $array['pasienId'];
        if( $frdate != 'all' )
        	$where['frdate'] = $frdate;
        if( $todate != 'all' )
        	$where['todate'] = $todate;	
     
        $total = $this->registrasi_model->getCount($where);
        
        $this->load->library('pagination');
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
       	$config['base_url'] = site_url('klinik/rekamMedis/view/pasienId/'.$array['pasienId'].'/frdate/'.$array['frdate'].'/todate/'.$array['todate'].'/page/');
        $config['uri_segment'] = 10;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->registrasi_model->getList($where, $limit, $array['page']);
        $this->_data['dataView'] = $dataView;
        $this->_data['uri_to_assoc'] = $array;

        $this->_view ( 'klinik/rekamMedisView' );
    }
    
}